package the.workshop.milktea.interfaces.api;

import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.web.APIResult;
import the.workshop.milktea.infrastructure.model.DeviceCollect;
import the.workshop.milktea.interfaces.req.DeviceAddReq;
import the.workshop.milktea.interfaces.req.DeviceLockStatusReq;
import the.workshop.milktea.interfaces.resp.DeviceLockStatusResp;
import the.workshop.milktea.service.DeviceCollectService;
import the.workshop.milktea.service.DeviceService;
import the.workshop.milktea.service.dto.DeviceAddDTO;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "Tea")
@RestController
@RequestMapping("/api/tea/device")
public class ApiDeviceController {

    @Resource
    private DeviceService deviceService;

    @Resource
    private DeviceCollectService deviceCollectService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody DeviceAddReq req) {
        DeviceAddDTO dto = new DeviceAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorName("from APP");
        return APIResult.success(deviceService.add(dto));
    }

    @PostMapping("/get-lock-status")
    public APIResult<DeviceLockStatusResp> getLockStatus(@RequestBody @Valid DeviceLockStatusReq req) {
        return APIResult.success(deviceService.getLockStatus(req.getMac()));
    }

    @PostMapping("/collect")
    public APIResult<Boolean> collect(@RequestBody List<DeviceCollect> data) {
        if (CollectionUtils.isEmpty(data)) {
            return APIResult.success(false);
        }

        for (DeviceCollect item : data) {
            deviceCollectService.add(item);
        }
        return APIResult.success(true);
    }
}
